<?php
//数据按照树结构的要求进行排序
use Illuminate\Support\Facades\DB;
use Ramsey\Uuid\Uuid;

function authtree($auth_menu)
{
    return resort($auth_menu);
}
function resort($data, $parentid = 0, $level = 0)
{
    static $ret = array();
    foreach ($data as $k => $v) {
        if (is_object($v)) {
            $arr = (array)$v;
            if ($arr['pid'] == $parentid) {
                $arr['level'] = $level;
                $ret[] = $arr;
                resort($data, $arr['id'], $level + 1);
            }
        }
    }
    return $ret;
}
function has_child($id)
{
    $rs = DB::select("select count(*) c from zhyl_sys_auth where auth_pid=?", [$id]);
    $aa = sqlcount($rs);
    return sqlcount($rs) > 0 ?? false;
}

/**
 * @param 查数量
 * @return mixed
 */
function sqlcount($rs)
{
    return $rs[0]->c;
}


/**
 * 格式化打印数组
 */
function p($arr)
{
    echo '<pre>';
    print_r($arr);
    echo '</pre>';
}

/*
 * 数组写入文件
 * @param $data 数据源
 * @param $type 1单条 2数组
 */
function wlog1($data)
{
    //    if($type==1){
    $file = fopen("single.txt", 'w');
    fwrite($file, $data);
    fclose($file);
    //    }
    //    else if($type==2){
    //        $fp = fopen('test_arr.txt', 'w+');
    //        fwrite($fp, var_export($data, true));
    //        fclose($fp);
    //    }
}

function wlog2($data)
{
    $fp = fopen('test_arr.txt', 'w+');
    fwrite($fp, var_export($data, true));
    fclose($fp);
}

/**
 * @param $ids
 * 将数组转换成字符串返回，用语sql中的in 使用
 */
function implode_arrs($ids)
{
    return "'" . implode("','", $ids) . "'";
}

function returnMsg()
{
    return json_encode(array("msg" => "success", "flag" => 1, "data" => ""));
}
function returnFail()
{
    return json_encode(array("msg" => "fail", "flag" => 0, "data" => ""));
}
function uploadfile($request)
{
    $up_file = $request->file;
    $ext = $up_file->getClientOriginalExtension();
    $realpath = $up_file->getRealPath();
    //$file = $request->username .'/'. uniqid().'.'.$ext;
    $file = $request->username . '-' . $request->account . '-' . date('Ymd', time()) . '.' . $ext;

    $bool = Storage::disk('uploads')->put($file, file_get_contents($realpath));
    //$sql = "update 企业微信人员数据 set 是否上传='是' where 姓名='$request->username' and 工号='$request->account'";
    $where = array();
    $where['姓名'] = $request->username;
    $where['工号'] = $request->account;
    $where['日期'] = date('Y-m-d', time());
    $ups = array();
    $ups['是否上传'] = '是';
    DB::connection('peralert')->table("企业微信人员数据")->where($where)->update($ups);
    return $realpath;
}

/**
 * 报修功能函数--公共
 */
function bxuploadfile($request)
{

    $up_file = $request->file;
    $ext = $up_file->getClientOriginalExtension();
    $realpath = $up_file->getRealPath();
    //$file = $request->username .'/'. uniqid().'.'.$ext;
    $file = uniqid() . '-' . date('Ymd', time()) . '.' . $ext; //$request->username .'-' . $request->account .'-'. date('Ymd',time()) .'.'.$ext;

    $bool = Storage::disk('ssbxuploads')->put($file, file_get_contents($realpath));
    //$sql = "update 企业微信人员数据 set 是否上传='是' where 姓名='$request->username' and 工号='$request->account'";
    $where = array();
    $where['姓名'] = $request->username;
    $where['工号'] = $request->account;
    $where['日期'] = date('Y-m-d', time());
    $ups = array();
    $ups['是否上传'] = '是';
    DB::connection('peralert')->table("企业微信人员数据")->where($where)->update($ups);
    return $file;
}
function bxuploadfile2($request)
{

    $up_file = $request->file;
    $ext = $up_file->getClientOriginalExtension();
    $realpath = $up_file->getRealPath();
    //$file = $request->username .'/'. uniqid().'.'.$ext;
    $file = uniqid() . '-' . date('Ymd', time()) . '.' . $ext; //$request->username .'-' . $request->account .'-'. date('Ymd',time()) .'.'.$ext;

    $bool = Storage::disk('bpmshuploads')->put($file, file_get_contents($realpath));

    return $file;
}



//用来保存应届生的简历
function uploadfile2($request)
{
    $up_file = $request->file;
    $ext = $up_file->getClientOriginalExtension();
    $realpath = $up_file->getRealPath();
    $uuid = Uuid::uuid1();
    $file = date('YmdHmi', time()) . '-' . $uuid . '.' . $ext;
    $bool = Storage::disk('uploadsresume')->put($file, file_get_contents($realpath));
    //return $realpath;
    return $file;
}


function getSession($request)
{
    return $request->session()->get('userinfo')[0];
}

//生成短信验证码
function generate_code($length = 6)
{
    $min = pow(10, ($length - 1));
    $max = pow(10, $length) - 1;
    return rand($min, $max);
}
function send_post($url, $post_data)
{

    $postdata = http_build_query($post_data);
    $options  = array('http' => array(
        'method' => 'POST', 'header' => 'Content-type:application/x-www-form-urlencoded', 'content' => $postdata, 'timeout' => 15 * 60 // 超时时间（单位:s）
    ));
    $context  = stream_context_create($options);
    $result   = file_get_contents($url, false, $context);

    return $result;
}
/**
 * 统一返回模板
 */
function return_msg($data, $flag, $msg)
{
    return array("data" => $data, "flag" => $flag, "msg" => $msg);
}

/**
 * 输出带参数的sql
 */
function print_binds()
{
    $bindings = $wtblist->getBindings();
    $sql = str_replace('?', '%s', $wtblist->toSql());
    $sql = sprintf($sql, ...$bindings);
    dd($sql);
}
